package com.example.day2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    ListView textView1;
    ArrayAdapter adpter;
    String[] aa = new String[]{"1","2","3","4","5","6"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView1=findViewById(R.id.text1);
        this.registerForContextMenu(textView1);
        adpter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,aa);
        textView1.setAdapter(adpter);
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                   ContextMenu.ContextMenuInfo menuInfo) {
        Log.v("####", "populate context menu");
        // set context menu title
        menu.setHeaderTitle("文件操作");
        // add context menu item
        menu.add(0, 1, Menu.NONE, "发送");
        menu.add(0, 2, Menu.NONE, "标记为重要");
        menu.add(0, 3, Menu.NONE, "重命名");
        menu.add(0, 4, Menu.NONE, "删除");
        SubMenu second = menu.addSubMenu(1, 10, 100, "二级");
        second.add(1, 12, 100, "二级1");
        second.add(1, 13, 110, "二级2");
    }
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // 得到当前被选中的item信息
           Log.e("#####",((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).id+"");
        switch (item.getItemId()) {
            case 1:
                // do something

                break;
            case 2:
                // do something
                break;
            case 3:
                // do something
                break;
            case 4:
                // do something
                int b= (int)((AdapterView.AdapterContextMenuInfo)item.getMenuInfo()).id;
                aa[b]="-1";
                adpter.notifyDataSetChanged();
                break;
            default:
                return super.onContextItemSelected(item);
        }
        return true;
    }

     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
     // TODO Auto-generated method stub
//        MenuInflater mInflater=new MenuInflater(this);
//         mInflater.inflate(R.menu.mymenu, menu);
//     return super.onCreateOptionsMenu(menu);
//         通过代码的方式来添加Menu
//         添加菜单项（组ID，菜单项ID，排序，标题）
         menu.add(0, 1, 300, "item1");
         menu.add(0, 2, 200, "item2");
        //添加子菜单
        SubMenu sub1 = menu.addSubMenu("setting");
        sub1.add(1, 3, 300, "item3");
        sub1.add(1, 4, 400, "item4");
         SubMenu sub2 = sub1.addSubMenu("setting");
         sub2.add(1, 5, 300, "item3");
         sub2.add(1, 6, 400, "item4");
        return true;
     }
    //重写OptionsItemSelected(MenuItem item)来响应菜单项(MenuItem)的点击事件（根据id来区分是哪个item）

    @Override
     public boolean onOptionsItemSelected(MenuItem item) {
                // Handle action bar item clicks here. The action bar will
              // automatically handle clicks on the Home/Up button, so long
              // as you specify a parent activity in AndroidManifest.xml.
                switch (item.getItemId()) {
                    case R.id.item1:
                            Toast.makeText(this, "点击列表1", Toast.LENGTH_SHORT).show();
                            break;
                  case R.id.item21:
                           Toast.makeText(this, "点击列表2-1", Toast.LENGTH_SHORT).show();
                          break;


                    case 1:
                        Toast.makeText(this, "开始游戏", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        Toast.makeText(this, "结束游戏", Toast.LENGTH_SHORT).show();
                        break;

                    case 3:
                        Toast.makeText(this, "声音設置", Toast.LENGTH_SHORT).show();
                        break;

                    case 4:
                        Toast.makeText(this, "背景設置", Toast.LENGTH_SHORT).show();
                        break;
                   default:
                         break;
                   }
            return super.onOptionsItemSelected(item);
           }
}
